package com.chy.blog_web.modular.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_web.core.util.PageAndParamUtil;
import com.chy.blog_web.modular.system.entity.SysMenu;
import com.chy.blog_web.modular.system.service.ISysMenuService;
import com.chy.blog_web.modular.web.entity.BoArticle;
import com.chy.blog_web.modular.web.entity.BoContent;
import com.chy.blog_web.modular.web.entity.BoIndexImage;
import com.chy.blog_web.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_web.modular.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：主页 controller
 * 作者：chenhuiyan
 * 时间：2020/03/05 22:07
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/index/";

    /**
     * 导航service
     */
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 内容service
     */
    @Autowired
    private IBoContentService boContentService;

    /**
     * 相册service
     */
    @Autowired
    private IBoPhotoAlbumService boPhotoAlbumService;

    /**
     * 文章标签service
     */
    @Autowired
    private IBoTagService boTagService;

    /**
     * 文章service
     */
    @Autowired
    private IBoArticleService boArticleService;

    /**
     * 首页展示照片service
     */
    @Autowired
    private IBoIndexImageService boIndexImageService;

    /**
     * 音乐service
     */
    @Autowired
    private IBoMusicService boMusicService;

    /**
     * 跳转主页面
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "index";
    }

    /**
     * 查询导航、LOGO标题、首页关于我、页脚、我的相册、
     * 文章标签（关联的文章总数）、站长推荐文章、文章点击排行、
     * 首页展示照片
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIndexData")
    public Map<String, Object> getIndexData(){
        Map<String, Object> res = new HashMap<>();
        try{
            //查询前台显示导航，并且按创建时间升序排序
            List<SysMenu> sysMenuList = sysMenuService.getBaseMapper()
                    .selectList(new QueryWrapper<SysMenu>()
                            .eq("type", 1)
                            .eq("is_show", 0)
                            .eq("del_flag", 0)
                            .orderByAsc("sort"));

            //查询LOGO标题，编号为：521f1cd04f9d4345af2e62213178d6ac
            BoContent logoTitle = boContentService.getById("521f1cd04f9d4345af2e62213178d6ac");

            //查询首页关于我，编号为：4029b657e3f54ca79478e43e238d1f2e
            BoContent indexAboutMe = boContentService.getById("4029b657e3f54ca79478e43e238d1f2e");

            //查询页脚，编号为：5d8530c950d243e18e57b74190aa2d94
            BoContent indexFooter = boContentService.getById("5d8530c950d243e18e57b74190aa2d94");

            //查询友情连接，编号为：26cd46e996704076aea248ff9c3a90b9
            BoContent friendshipUrl = boContentService.getById("26cd46e996704076aea248ff9c3a90b9");

            //查询我的相册,设置为显示的、不上锁的、按创建时间降序排列,最多查6条
            List<BoPhotoAlbum> boPhotoAlbumList = boPhotoAlbumService.getBaseMapper()
                    .selectList(new QueryWrapper<BoPhotoAlbum>()
                            .eq("is_show", 0)
                            .eq("is_lock", 0)
                            .eq("del_flag", 0)
                            .orderByDesc("create_date")
                            .last("LIMIT 6"));

            //查询文章标签（关联的文章总数）
            List<Map<String , Object>> boTagList = boTagService.getBoTagList();

            //查询站长推荐、通过审核文章，按创建时间降序,最多查10条
            List<BoArticle> boArticleTopList = boArticleService.getBaseMapper()
                    .selectList(new QueryWrapper<BoArticle>()
                            .eq("is_top", 1)
                            .eq("is_pass", 1)
                            .eq("del_flag", 0)
                            .orderByDesc("create_date")
                            .last("LIMIT 10"));

            //文章点击排行、通过审核文章,按阅读、赞、时间排序。最多查10条
            List<BoArticle> articleClickList = boArticleService.getBaseMapper()
                    .selectList(new QueryWrapper<BoArticle>()
                            .eq("is_pass", 1)
                            .eq("del_flag", 0)
                            .orderByDesc("read_num")
                            .orderByDesc("praise_num")
                            .orderByDesc("create_date")
                            .last("LIMIT 10"));

            //查询首页展示照片列表
            List<BoIndexImage> boIndexImageList = boIndexImageService.getBaseMapper()
                    .selectList(new QueryWrapper<BoIndexImage>()
                            .eq("del_flag", 0)
                            .eq("is_show", 0)
                            .orderByAsc("sort"));

            res.put("sysMenuList", sysMenuList);
            res.put("logoTitle", logoTitle);
            res.put("indexAboutMe", indexAboutMe);
            res.put("indexFooter", indexFooter);
            res.put("friendshipUrl", friendshipUrl);
            res.put("boPhotoAlbumList", boPhotoAlbumList);
            res.put("boTagList", boTagList);
            res.put("boArticleTopList", boArticleTopList);
            res.put("articleClickList", articleClickList);
            res.put("boIndexImageList", boIndexImageList);
            res.put("state", "200");
            res.put("message", "初始化成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }

        return res;
    }

    /**
     * 查询页脚
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getFooterData")
    public Map<String, Object> getFooterData(){
        Map<String, Object> res = new HashMap<>();
        try{
            //查询页脚，编号为：5d8530c950d243e18e57b74190aa2d94
            BoContent indexFooter = boContentService.getById("5d8530c950d243e18e57b74190aa2d94");
            res.put("indexFooter", indexFooter);
            res.put("state", "200");
            res.put("message", "初始化成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }

        return res;
    }

    /**
     * 查询文章列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getArticleList")
    public Map<String, Object> getArticleList(){
        Map<String, Object> res = new HashMap<>();
        Map<String, Object> params = PageAndParamUtil.getParams();
        try {
            List<Map<String, Object>> articleList = boArticleService.list(params);
            Integer count = boArticleService.getCount(params); //获取总数
            res.put("state", "200");
            res.put("message", "查询文章列表成功");
            res.put("articleList", articleList);
            res.put("count", count);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }
        return res;
    }

    /**
     * 查询音乐列表
     * @return
     */
    @RequestMapping(value = "/getMusicList")
    @ResponseBody
    public Map<String, Object> getMusicList(){
        Map<String, Object> res = new HashMap<>();
        try{
            List<Map<String, Object>> list = boMusicService.getMusicList();
            res.put("list", list);
            res.put("state", "200");
            res.put("message", "音乐初始化成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "音乐初始化失败");
        }
        return res;
    }


}
